package com.yueze.demo1.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;

/**
 * @author Administrator
 * @create 2020-07-28
 * 15:29
 * @desc 裁剪图片
 **/
@Controller
public class CutController {

    /**
     * 图片切割
     * @param x  目标切片坐标 X轴起点
     * @param y  目标切片坐标 Y轴起点
     * @param w  目标切片 宽度
     * @param h  目标切片 高度
     */
    @RequestMapping("/cut")
    public String cutImage(Model model,String imagename, HttpSession session, int x , int y , int w, int h){
        String subPath1 = "" ;
        String prex = "";
        try {

            Image img;
            ImageFilter cropFilter;
            // 读取源图像
            String imagePath = (String) session.getAttribute("path2");
            prex = imagePath.substring(imagePath.indexOf(".")+1);
            //subPath1= "C:"+File.separator+"javaproject"+File.separator+"siruan"+File.separator+"save"+File.separator+imagename+".jpeg";
            subPath1= File.separator+"hzsr"+File.separator+"web"+File.separator+"save"+File.separator+"3"+imagename+"."+prex;

            BufferedImage bi = ImageIO.read(new File(imagePath));
            int srcWidth = bi.getWidth();      // 源图宽度
            int srcHeight = bi.getHeight();    // 源图高度
            //若原图大小大于切片大小，则进行切割
            if (srcWidth >= w && srcHeight >= h) {
                Image image = ((Image) bi).getScaledInstance(srcWidth, srcHeight,Image.SCALE_DEFAULT);

                int x1 = x*srcWidth/400;
                int y1 = y*srcHeight/270;
                int w1 = w*srcWidth/400;
                int h1 = h*srcHeight/270;

                cropFilter = new CropImageFilter(x1, y1, w1, h1);
                img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), cropFilter));
                BufferedImage tag = new BufferedImage(w1, h1,BufferedImage.TYPE_INT_RGB);
                Graphics g = tag.getGraphics();
                g.drawImage(img, 0, 0, null); // 绘制缩小后的图
                g.dispose();
                // 输出为文件
                //ImageIO.write(tag, "jpeg", new File(subPath1));
                ImageIO.write(tag, prex, new File(subPath1));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String name = (String) session.getAttribute("name");
        String fileName1 = (String) session.getAttribute("path1");
        model.addAttribute("path2",name);
        model.addAttribute("path1",fileName1);
        String cutName = "3"+imagename + "."+prex;
        System.out.println(cutName);
        model.addAttribute("cut1",cutName);
        return "index3";
    }
}
